/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gac.gacmod.visual;

import org.gac.gacmod.model.CompositeStep;
import org.gac.gacmod.model.Step;
import org.netbeans.api.visual.anchor.AnchorFactory;
import org.netbeans.api.visual.anchor.AnchorShape;
import org.netbeans.api.visual.router.RouterFactory;
import org.netbeans.api.visual.widget.ConnectionWidget;

/**
 *
 * @author Tomek
 */
public class RepeatStepWidget extends CompositeStepWidget
{
    ConnectionWidget _downArrow;
    ConnectionWidget _upArrow;
    DropAreaWidget _dropArea;

    public RepeatStepWidget( BoxScene scene, Step modelStep )
    {
        super(scene, modelStep);

        _dropArea = new DropAreaWidget(scene, true,
                (CompositeStep) _modelStep, 0);
        _clientArea.addChild(_dropArea);

        _downArrow = new ConnectionWidget (scene);
        _downArrow.setSourceAnchor (AnchorFactory.createDirectionalAnchor(
                _headerVFlow, AnchorFactory.DirectionalAnchorKind.VERTICAL));
        _downArrow.setTargetAnchor (AnchorFactory.createDirectionalAnchor(
                _bottomWidget, AnchorFactory.DirectionalAnchorKind.VERTICAL));
        //_downArrow.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
        //edge.setMaximumSize( new Dimension( 2,2));
        _downArrow.setRouter (RouterFactory.createOrthogonalSearchRouter() );

        _scene.getConnectionLayer().addChild(_downArrow);

        _upArrow = new ConnectionWidget (scene);
        _upArrow.setSourceAnchor (AnchorFactory.createDirectionalAnchor(
                _bottomWidget, AnchorFactory.DirectionalAnchorKind.HORIZONTAL));
        _upArrow.setTargetAnchor (AnchorFactory.createDirectionalAnchor(
                _headerVFlow, AnchorFactory.DirectionalAnchorKind.HORIZONTAL));
        _upArrow.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
        //edge.setMaximumSize( new Dimension( 2,2));
        _upArrow.setRouter ( new RepeatEdgeRouter( _dropArea ) );

        _scene.getConnectionLayer().addChild(_upArrow);
    }
    
    @Override
    public void addInnerStep(StepWidgetBase step)
    {
        step._parent = this;
        _clientArea.addChild( step );

        _downArrow.setVisible(false);
        _upArrow.setVisible(false);
        _upArrow.removeFromParent();

        _dropArea.removeFromParent();
        _scene.removeDropAreaWidget( _dropArea );

        ConnectionWidget downArrow1 = new ConnectionWidget (_scene);
        downArrow1.setSourceAnchor (AnchorFactory.createDirectionalAnchor(
                _headerVFlow, AnchorFactory.DirectionalAnchorKind.VERTICAL));
        downArrow1.setTargetAnchor (AnchorFactory.createDirectionalAnchor(
                step, AnchorFactory.DirectionalAnchorKind.VERTICAL));
        //downArrow1.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
        downArrow1.setRouter (RouterFactory.createOrthogonalSearchRouter() );
        _scene.getConnectionLayer().addChild( downArrow1 );

        ConnectionWidget downArrow2 = new ConnectionWidget (_scene);
        downArrow2.setSourceAnchor (AnchorFactory.createDirectionalAnchor(
                step, AnchorFactory.DirectionalAnchorKind.VERTICAL));
        downArrow2.setTargetAnchor (AnchorFactory.createDirectionalAnchor(
                _bottomWidget, AnchorFactory.DirectionalAnchorKind.VERTICAL));
        //downArrow2.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
        downArrow2.setRouter (RouterFactory.createOrthogonalSearchRouter() );
        _scene.getConnectionLayer().addChild( downArrow2 );

         ConnectionWidget upArrow = new ConnectionWidget (_scene);
        upArrow.setSourceAnchor (AnchorFactory.createDirectionalAnchor(
                _bottomWidget, AnchorFactory.DirectionalAnchorKind.HORIZONTAL));
        upArrow.setTargetAnchor (AnchorFactory.createDirectionalAnchor(
                _headerVFlow, AnchorFactory.DirectionalAnchorKind.HORIZONTAL));
        upArrow.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
        //edge.setMaximumSize( new Dimension( 2,2));
        upArrow.setRouter ( new RepeatEdgeRouter( step ) );

        _scene.getConnectionLayer().addChild(upArrow); 

    }

    @Override
    public void removeInnerStep(StepWidgetBase step)
    {
        ((CompositeStep) _modelStep).RemoveStep(step._modelStep);
    }
}
